package com.shuyun.xxx.guava.practice;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.*;

/**
 * Created by goodb on 2017/5/21.
 */
public class ThreadPoolExcutorTest {

    private static final Logger LOGGER = LoggerFactory.getLogger(ThreadPoolExcutorTest.class);
    @Test
    public void run(){
        ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 15, 60, TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(100), new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                LOGGER.error("reject submit thread to thread pool");
            }
        });
        for(int i = 0; i < 10; i++){
            executor.submit(() -> {
                System.out.println(Thread.currentThread().getName());
            });
        }

        for(int i = 0; i < 5; i++){
            Future<String> task = executor.submit(() -> {
                return "success";
            });
            String res = "";
            try {
                res = task.get();
            } catch (Exception e) {
                LOGGER.error("get result error", e);
            }
            System.out.println(res);
        }

    }
}
